package com.itheima.web;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
    private BrandService brandService = new BrandServiceImpl();
    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<Brand> brands = brandService.selectAll();
        String jsonString= JSON.toJSONString(brands);
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串
        Brand brand = JSON.parseObject(params, Brand.class);
        brandService.add(brand);
        resp.getWriter().write("success");
    }
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串
        Brand brand = JSON.parseObject(params, Brand.class);
        brandService.update(brand);
        resp.getWriter().write("success");
    }
    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串
        int[] ids= JSON.parseObject(params, int[].class);
        brandService.deleteByIds(ids);
        resp.getWriter().write("success");
    }
    public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String _currentPage=req.getParameter("currentPage");
        String _pageSize=req.getParameter("pageSize");
        int currentPage=Integer.parseInt(_currentPage);
        int pageSize=Integer.parseInt(_pageSize);
        PageBean<Brand> pageBean=brandService.selectByPage(currentPage,pageSize);
        String jsonString= JSON.toJSONString(pageBean);
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String _id = req.getParameter("id");
        int id=Integer.parseInt(_id);
        brandService.deleteById(id);
        resp.getWriter().write("success");
    }
    public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String _currentPage=req.getParameter("currentPage");
        String _pageSize=req.getParameter("pageSize");
        int currentPage=Integer.parseInt(_currentPage);
        int pageSize=Integer.parseInt(_pageSize);

        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串
        Brand brand= JSON.parseObject(params, Brand.class);

        PageBean<Brand> pageBean=brandService.selectByPageAndCondition(currentPage,pageSize,brand);
        String jsonString= JSON.toJSONString(pageBean);
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }
}
